<template>
  <div>
    <h1>我的拼团</h1>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="orderId" label="id" width="180" />
      <el-table-column prop="orderNo" label="订单编号" width="180" />
      <el-table-column prop="orderStatus" label="订单状态">
        <template #default="scope">
          <el-tag v-if="scope.row.orderStatus == 0">待支付</el-tag>
          <el-tag v-if="scope.row.orderStatus == 1" type="danger"
            >已支付/待发货</el-tag
          >
          <el-tag v-if="scope.row.orderStatus == 2" type="info">已发货</el-tag>
          <el-tag v-if="scope.row.orderStatus == 3" type="success"
            >已完成</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="orderAmount" label="订单金额" />
      <el-table-column prop="payTime" label="支付时间" />
      <el-table-column prop="orderName" label="订单类型" />
      <el-table-column prop="groupId" label="参团id" />
      <el-table-column prop="userName" label="用户名称" />
      <el-table-column prop="prodName" label="商品信息" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            style="margin-left: 16px"
            @click="ShowOrder(scope.row)"
          >
            我的拼团详情
          </el-button>
          <el-button
            type="danger"
            style="margin-left: 16px"
            @click="OnDeleteOrder(scope.row)"
          >
            取消订单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <div class="demonstration"></div>
      <el-pagination
        v-model:current-page="listFrom.pageNum"
        v-model:page-size="listFrom.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-drawer v-model="drawer2" :direction="direction">
        <template #header>
          <h4>我的拼团详情页面</h4>
        </template>
        <hr />
        <el-card style="max-width: 580px">
          <el-descriptions>
            <el-descriptions-item label="订单编号:">{{
              form.orderNo
            }}</el-descriptions-item>
            <el-descriptions-item label="订单状态:">
              <el-tag v-if="form.orderStatus == 0">待支付</el-tag>
              <el-tag v-if="form.orderStatus == 1" type="danger"
                >已支付/待发货</el-tag
              >
              <el-tag v-if="form.orderStatus == 2" type="info">已发货</el-tag>
              <el-tag v-if="form.orderStatus == 3" type="success"
                >已完成</el-tag
              >
            </el-descriptions-item>
            <el-descriptions-item label="订单金额:">{{
              form.orderAmount
            }}</el-descriptions-item>
            <el-descriptions-item label="支付时间:">{{
              form.payTime
            }}</el-descriptions-item>
            <el-descriptions-item label="订单类型:">{{
              form.orderName
            }}</el-descriptions-item>
            <el-descriptions-item label="参团id:">{{
              form.groupId
            }}</el-descriptions-item>
            <el-descriptions-item label="用户名称:">{{
              form.userName
            }}</el-descriptions-item>
            <el-descriptions-item label="商品信息:">{{
              form.prodName
            }}</el-descriptions-item>
          </el-descriptions>
        </el-card>

        <template #footer>
          <div style="flex: auto">
            <el-button @click="drawer2 = false" type="info">返回</el-button>
          </div>
        </template>
      </el-drawer>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getGroupOrderList, deleteOrder } from "../api/api";
import { ElMessage, ElMessageBox } from "element-plus";

import type { DrawerProps } from "element-plus";
const tableData = ref([]);
const total = ref(0);
const direction = ref<DrawerProps["direction"]>("rtl");
const drawer2 = ref(false);
const listFrom = ref({
  pageNum: 1,
  pageSize: 2,
});
const form = ref({
  orderNo: "",
  orderStatus: null,
  orderAmount: null,
  payTime: "",
  orderName: "",
  groupId: null,
  userName: "",
  prodName: "",
});
const list = () => {
  getGroupOrderList(listFrom.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  listFrom.value.pageSize = val;
  list();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  listFrom.value.pageNum = val;
  list();
};
//详情
const ShowOrder = (row: any) => {
  form.value = row;
  drawer2.value = true;
};
//删除
const OnDeleteOrder = (row: any) => {
  ElMessageBox.confirm("请您三思啊，确定要删除吗?", "删除严重警告框", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "error",
  })
    .then(() => {
      deleteOrder(row.orderId).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("用户删除订单成功！");
          list();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消删除",
      });
    });
};
onMounted(() => {
  list();
});
</script>
